package com.qtcm.nas.fyx.huiyuanxinxi.web;

import com.qtcm.nas.fyx.chakanxiangqing.pojo.Package_Details;
import com.qtcm.nas.fyx.huiyuantianjia.pojo.Car_fyx;
import com.qtcm.nas.fyx.huiyuantianjia.pojo.MemberShip;
import com.qtcm.nas.fyx.huiyuantianjia.pojo.Orders_fyx;
import com.qtcm.nas.fyx.huiyuanxinxi.pojo.Card_Cz;
import com.qtcm.nas.fyx.huiyuanxinxi.pojo.Card_nk;
import com.qtcm.nas.fyx.huiyuanxinxi.pojo.Points_Records;
import com.qtcm.nas.fyx.huiyuanxinxi.service.IHyxxService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.List;

@Controller("hyxxController")
@RequestMapping("hyxx")
public class HyxxController {
    private int hyid;
    private String c_name;
    @Autowired
    @Qualifier("hyxxService")
    private IHyxxService hyxxService;

    @RequestMapping("findhyid")
    public String findhyid(String cxtj) {
        System.out.println(cxtj);
        if (cxtj != "") {
            cxtj = "%" + cxtj;
            List<Integer> findhyid = hyxxService.findhyid(cxtj);
            for (int f : findhyid) {
                System.out.println(f);
                hyid = f;
            }
            System.out.println("-------------------");
            return "redirect:/hyxx/findById";
        }
        return "user_Info";
    }

    @RequestMapping("findById")
    public String findByid(Model model) throws IOException {
        MemberShip byId = hyxxService.findById(hyid);
        c_name=byId.getC_name();
        String finddp = hyxxService.finddp(hyid);
        byId.setL_dianpu(finddp);
        System.out.println(byId);
        model.addAttribute("byId", byId);


        List<Car_fyx> car = hyxxService.findCar(hyid);
        model.addAttribute("car", car);

        //单独查手机号使的
        String phone = hyxxService.findPhone(hyid);


        List<Card_Cz> findchuzhi = hyxxService.findchuzhi(hyid);
        model.addAttribute("card",findchuzhi);


        List<Card_nk> nian = hyxxService.findnian(hyid);
        System.out.println("----------------------------------------");
        System.out.println(nian);
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        model.addAttribute("nian", nian);


        List<Package_Details> findlb = hyxxService.findlb(hyid);
        for (Package_Details lb:findlb) {
            lb.setC_phone(phone);
        }
        for (Package_Details lb:findlb){
            System.out.println(lb);
        }
        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
        model.addAttribute("lb", findlb);


        List<Orders_fyx> finddingd = hyxxService.finddingd(hyid);
        for (Orders_fyx o : finddingd) {
            String finddianp = hyxxService.finddianp(hyid);
            o.setC_dianpu(finddianp);
        }
        System.out.println(finddingd);
        model.addAttribute("list", finddingd);


        List<Points_Records> findjif = hyxxService.findjif(hyid);
        for (Points_Records p : findjif) {
            p.setC_name(byId.getC_name());
            p.setC_phone(phone);
        }
        System.out.println(findjif);
        model.addAttribute("jifenlist", findjif);
        return "user_Info";
    }

    @RequestMapping("updateUser")
    public String updateUser(String c_name, String c_phone, String l_dianpu, String c_zhucefs) {
        System.out.println(c_name);
        System.out.println(l_dianpu);
        System.out.println(c_phone);
        System.out.println(c_zhucefs);
        MemberShip memberShip = new MemberShip();
        memberShip.setC_name(c_name);
        memberShip.setC_phone(c_phone);
        memberShip.setL_dianpu(l_dianpu);
        memberShip.setC_phone(c_phone);
        memberShip.setC_zhucefs(c_zhucefs);
        memberShip.setHyid(hyid);
        System.out.println(memberShip);
        hyxxService.updateUser(memberShip);
        hyxxService.updatedianpu(memberShip);
        return "redirect:/hyxx/findById";
    }

    @RequestMapping("addCar")
    public String addCar(Car_fyx car_fyx) {
        car_fyx.setC_membernumber(hyid);
        System.out.println(car_fyx);
        hyxxService.addCar(car_fyx);
        return "redirect:/hyxx/findById";
    }

    @RequestMapping("editCar")
    public String editCar(Car_fyx car_fyx) {
        car_fyx.getC_carnumber();
        System.out.println(car_fyx);
        hyxxService.editCar(car_fyx);
        return "redirect:/hyxx/findById";
    }

    @RequestMapping("findOneCar/{c_ipnumber}")
    public String findOneCar(@PathVariable("c_ipnumber") String c_ipnumber, Model model) {
        System.out.println(c_ipnumber);
        Car_fyx oneCar = hyxxService.findOneCar(c_ipnumber);
        System.out.println(oneCar);
        model.addAttribute("oneCar", oneCar);
        return "edit_Car";
    }

    @RequestMapping("delCar/{delId}")
    public String delCar(@PathVariable("delId") String id) {
        System.out.println(id);
        hyxxService.delCar(id);
        return "redirect:/hyxx/findById";
    }

    @RequestMapping("findxs")
    public String findxs(Model model) {
        int id = 2;
        List<String> findxs = hyxxService.findxs(id);
        model.addAttribute("findxs", findxs);
        return "add_Pcard";
    }

    @RequestMapping("addCzCard")
    public String addCzCard(Card_Cz czcard) {
        czcard.setC_membernumber(hyid);
        System.out.println(czcard);
        hyxxService.addCzCard(czcard);
        return "redirect:/hyxx/findById";
    }

    @RequestMapping("nianxs")
    public String nianxs(Model model){
        int id = 2;
        List<String> findxs = hyxxService.findxs(id);
        System.out.println(findxs);
        model.addAttribute("findxs", findxs);

        List<String> ipnumber = hyxxService.findIpnumber(hyid);
        System.out.println(ipnumber);
        model.addAttribute("ipnumber",ipnumber);
        return "add_Ycard";
    }

    @RequestMapping("addNk")
    public String addNk(Card_nk card_nk){
        card_nk.setC_membernumber(hyid);
        card_nk.setC_name(c_name);
        hyxxService.addNk(card_nk);
        return "redirect:/hyxx/findById";
    }

    @RequestMapping("cardcz")
    public String pluscz(Model model){
        int id=2;
        List<String> findxs = hyxxService.findxs(id);
        System.out.println(findxs);
        model.addAttribute("id",hyid);
        model.addAttribute("findxs", findxs);
        return "pcard_Recharge";
    }

    @RequestMapping("kpcz")
    public String kpcz(Card_Cz card_cz){
        int money1 = hyxxService.findMoney(card_cz.getC_phone());
        money1=money1+card_cz.getMoney();
        System.out.println(card_cz);
        card_cz.setMoney(money1);
        hyxxService.czmoney(card_cz);
        return "redirect:/hyxx/findById";
    }

    @RequestMapping("cardsj")
    public String cardsj(Model model,String c_phone){
        int id=2;
        List<String> findxs = hyxxService.findxs(id);
        System.out.println(findxs);
        model.addAttribute("c_phone",c_phone);
        model.addAttribute("findxs", findxs);
        return "pcard_Upgrade";
    }

    @RequestMapping("kpsj")
    public String kpsj(Card_Cz card_cz){
        if (card_cz.getPcard()==1){
            card_cz.setStr("白金储值卡");
        }else if (card_cz.getPcard()==2){
            card_cz.setStr("黄钻储值卡");
        }else if(card_cz.getPcard()==3){
            card_cz.setStr("红钻储值卡");
        }
        System.out.println(card_cz);
        int money1 = hyxxService.findMoney(card_cz.getC_phone());
        int money=card_cz.getMoney();
        money=money1+card_cz.getMoney();
        System.out.println(money);
        card_cz.setMoney(money);
        hyxxService.kpsj(card_cz);
        return "redirect:/hyxx/findById";
    }

    @RequestMapping("lbtz")
    public String lbtz(Model model,String c_phone){
        List<Package_Details> findlbname = hyxxService.findlbname();
        model.addAttribute("lb",findlbname);
        int id=2;
        List<String> findxs = hyxxService.findxs(id);
        System.out.println(findxs);
        model.addAttribute("findxs", findxs);
        List<String> ipnumber = hyxxService.findIpnumber(hyid);
        System.out.println(ipnumber);
        model.addAttribute("ipnumber",ipnumber);
        model.addAttribute("c_phone",c_phone);
        return "add_Spree";
    }

    @RequestMapping("tjlb")
    public String tjlb(Package_Details package_details){
        if (package_details.getL_Gift_bag_name().equals("超级大礼包")){
                package_details.setL_money(8000);
        }else {
                package_details.setL_money(1);
        }
        package_details.setL_startdate("20021212");
        package_details.setL_enddate("20221212");
        package_details.setL_Gift_pack_service("胖子踩背");
        package_details.setC_membernumber(hyid);
        System.out.println(package_details);
        hyxxService.tjlb(package_details);

        return "redirect:/hyxx/findById";
    }
}
